package com.shangyitong.yygh_user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shangyitong.Result;
import com.shangyitong.client.DictFeignClient;
import com.shangyitong.yygh.enums.AuthStatusEnum;
import com.shangyitong.yygh.model.user.Patient;
import com.shangyitong.yygh.model.user.UserInfo;
import com.shangyitong.yygh.vo.user.UserInfoQueryVo;
import com.shangyitong.yygh_user.mapper.PatientMapper;
import com.shangyitong.yygh_user.mapper.UserInfoMapper;
import com.shangyitong.yygh_user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserService {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public Page<UserInfo> getPageList(Integer page, Integer limit, UserInfoQueryVo userInfoQueryVo) {
        Long total = userInfoMapper.getTotal();
        Integer start = (page - 1) * limit;
        List<UserInfo> userInfoLists = userInfoMapper.getRecords(start, limit, userInfoQueryVo);
        userInfoLists.forEach(item -> {
            String statusName = getStatusName(item.getStatus());
            String statusNameByStatus = AuthStatusEnum.getStatusNameByStatus(item.getAuthStatus());
            item.getParam().put("statusString", statusName);
            item.getParam().put("authStatusString", statusNameByStatus);
        });
        Page<UserInfo> pages = new Page<>();
        pages.setRecords(userInfoLists);
        pages.setTotal(total);
        return pages;
    }

    @Override
    public Map<String, Object> showUserDetail(Long id) {
        Map<String, Object> userMap = new HashMap<>();
        UserInfo userInfo = userInfoMapper.selectById(id);
        List<Patient> patients = patientMapper.selectList(new QueryWrapper<Patient>().eq("user_id", id));
        patients.forEach(item -> {
            item.getParam().put("certificatesTypeString", dictFeignClient.getSingleName(item.getCertificatesType()));
        });
        userMap.put("userInfo", userInfo);
        userMap.put("patientList", patients);
        return userMap;
    }

    //返回状态值
    private static String getStatusName(Integer status) {
        if (status == 1) {
            return "正常";
        }
        return "锁定";
    }
}
